﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Edit_Entities
{
    public partial class EditLend : System.Web.UI.Page
    {
        static public Guid Saveitem = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void USer2ComboBox_DataBound(object sender, EventArgs e)
        {
            USer2ComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }

        protected void Customer2ComboBox_DataBound(object sender, EventArgs e)
        {
            Customer2ComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }

        protected void Item2ComboBox_DataBound(object sender, EventArgs e)
        {
            Item2ComboBox.Items.Insert(0, new ListItem("--ALL--", "%"));
        }


        protected void LendDetailsView_DataBound(object sender, EventArgs e)
        {
            Label user = (Label)LendDetailsView.FindControl("UserLabel");
            Label customer = (Label)LendDetailsView.FindControl("CustomerLabel");
            Label item = (Label)LendDetailsView.FindControl("ItemLabel");

            if (user != null && customer != null && item != null)
            {
                Saveitem = new Guid(item.Text);
                UserBLL userBLL = new UserBLL();
                CustomerBLL customerBLL = new CustomerBLL();
                LendItemBLL lendItemBLL = new LendItemBLL();


                try
                {
                    user.Text = userBLL.userGetByNum(new Guid(user.Text))[0].user_name;
                    customer.Text = customerBLL.CustomerGetByNum(new Guid(customer.Text))[0].customer_name;
                    item.Text = lendItemBLL.LendItemByNum(new Guid(item.Text))[0].lend_item_serial;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event

                }

            }
        }



        protected void LendDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            CheckBox Available = (CheckBox)LendDetailsView.FindControl("AvailableCheckBox");
            if (Available.Checked)
            {
                LendItemBLL ItemBLL = new LendItemBLL();
                LendBLL LendBLL = new LendBLL();
                CustomerBLL CustomerBLL = new CustomerBLL();
                Guid ItemNum = LendBLL.LendGetByNum(new Guid(LendDetailsView.DataKey.Value.ToString()))[0].lend_item_num;
                ItemBLL.LendItemSetAvailable(ItemNum);
                Guid CustomerNum = LendBLL.LendGetByNum(new Guid(LendDetailsView.DataKey.Value.ToString()))[0].lend_customer;

                string ItemSerial = ItemBLL.LendItemByNum(ItemNum)[0].lend_item_serial;
                string customer = CustomerBLL.CustomerGetByNum(CustomerNum)[0].customer_name;

                //log Event
                string[] Parameters = { ItemSerial, customer };
                MyEvent ev = History.EventDecorator(History.ActionType.ReturnLend, Parameters, DateTime.Now);
                History.AddEvent(ev);
            }
        }




        protected void LendDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            MailPanel1.Visible = false;
            SentLabel.Visible = false;
            if (e.CommandName == "MailLend")
            {

                CustomerRadioButton.Enabled = true;
                UserRadioButton.Enabled = true;
                UserComboBox.Enabled = true;
                SentLabel.Visible = false;
                NotesTextBox.Text = "";
                NotesTextBox.Enabled = true;
                SendButton.Enabled = true;
                if (MailPanel1.Visible == false)
                {
                    MailPanel1.Visible = true;
                    SendButton.Focus();
                }
                else MailPanel1.Visible = false;
                LendDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            }
            
            if (e.CommandName == "Edit")
            {

            }

        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                MailPanel1.Visible = false;
                SentLabel.Focus();
                MailPanel1.Visible = false;
                LendBLL LendBLL = new LendBLL();
                CustomerBLL CustomerBLL = new CustomerBLL();
                UserBLL UserBLL = new UserBLL();
                LendItemBLL ItemBLL = new LendItemBLL();
                string EmailAddress;
                string Subject = "Entity Details have been sent to you by The WareHouse";
                Guid Key = new Guid(LendDetailsView.DataKey.Value.ToString());
                Guid ItemKey = LendBLL.LendGetByNum(Key)[0].lend_item_num;
                Guid UserKey = LendBLL.LendGetByNum(Key)[0].lend_user;
                Guid CustomerKey = LendBLL.LendGetByNum(Key)[0].lend_customer;
                DateTime LendDateStart = LendBLL.LendGetByNum(Key)[0].lend_start_date;
                DateTime LendDateEnd = LendBLL.LendGetByNum(Key)[0].lend_end_date;

                String ItemSerial = ItemBLL.LendItemByNum(ItemKey)[0].lend_item_serial;
                String UserName = UserBLL.userGetByNum(UserKey)[0].user_name;
                String CustomerName = CustomerBLL.CustomerGetByNum(CustomerKey)[0].customer_name;

                String[] fields = { "Component Serial", "Lend Start Date", "Lend End Date", "Customer Name", "Lending User" };
                String[] values = { ItemSerial, LendDateStart.ToShortDateString(), LendDateEnd.ToShortDateString(), CustomerName, UserName };

                String EntityString = Mail.HTMLEntityDecorator("Lend Details", fields, values);
                if (CustomerRadioButton.Checked)
                {
                    string CustomerMail = CustomerBLL.CustomerGetByNum(CustomerKey)[0].customer_email;
                    EmailAddress = CustomerMail;

                }
                else
                {

                    string UserMail = UserBLL.userGetByNum(new Guid(UserComboBox.SelectedValue))[0].user_email;
                    EmailAddress = UserMail;
                }

                Boolean res = Mail.SendMail(EmailAddress, Subject, Mail.SenderNotes(NotesTextBox.Text) + EntityString);
                if (res == false)
                {
                    SentLabel.Text = "Mail Failed";
                }
            }

            catch (Exception)
            {
                SentLabel.Text = "Mail Failed";

            }

            finally
            {
                /*
                //TODO:Dispose;
                CustomerRadioButton.Enabled = false;
                UserRadioButton.Enabled = false;
                NotesTextBox.Enabled = false;
                SendButton.Enabled = false;
                UserComboBox.Enabled = false;*/
                SentLabel.Visible = true;
                MailPanel1.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SentLabel.Visible = false;
            MailPanel1.Visible = false;
        }

        protected void AvailableCheckBox_DataBinding(object sender, EventArgs e)
        {
            LendItemBLL IBLL = new LendItemBLL();
            bool available = IBLL.LendItemByNum(Saveitem)[0].lend_item_available;
            if (available == true)
            {
                try
                {
                    CheckBox Available = (CheckBox)LendDetailsView.FindControl("AvailableCheckBox");
                    Available.Checked = true;
                    Available.Enabled = false;
                }
                catch (Exception)
                {
                }
            }
        }


    }
}
